#include "kernel_operator.h"

constexpr int32_t BUFFER_NUM = 2;
class KernelComplex {
public:
    __aicore__ inline KernelComplex() {}
    __aicore__ inline void Init(GM_ADDR x, GM_ADDR y, GM_ADDR z,const ComplexTilingData &tiling_data) 
    {
        this->tiling=tiling_data;
        uint64_t coreNum = AscendC::GetBlockIdx();
        uint64_t globalBufferIndex = tiling.bigCoreDataNum * AscendC::GetBlockIdx();
        this->total = tiling.inputDataNum;
        this->ubPartDataNum = tiling.ubPartDataNum;
        if (tiling.tailBlockNum!=0) 
        {
          if (coreNum < tiling.tailBlockNum) 
          { 
            this->coreDataNum = tiling.bigCoreDataNum;
            this->tileNum = tiling.bigCoreLoopNum;
            this->tailDataNum = tiling.bigCoreTailDataNum;
          }
          else 
          { 
            this->coreDataNum = tiling.smallCoreDataNum;
            this->tileNum = tiling.smallCoreLoopNum;
            this->tailDataNum = tiling.smallCoreTailDataNum;
            globalBufferIndex -= (tiling.bigCoreDataNum - tiling.smallCoreDataNum) * (AscendC::GetBlockIdx() - tiling.tailBlockNum);
          }
        }
        else
        {
          this->coreDataNum = tiling.smallCoreDataNum;
          this->tileNum = tiling.smallCoreLoopNum;
          this->tailDataNum = tiling.smallCoreTailDataNum;
          globalBufferIndex = tiling.smallCoreDataNum * AscendC::GetBlockIdx();
        }
          
        xGm.SetGlobalBuffer((__gm__ float *)x + globalBufferIndex, this->coreDataNum);
        yGm.SetGlobalBuffer((__gm__ float *)y + globalBufferIndex, this->coreDataNum);

        zGm.SetGlobalBuffer((__gm__ float *)z + globalBufferIndex*2, this->coreDataNum*2);

        pipe.InitBuffer(inQueueX, BUFFER_NUM, this->ubPartDataNum * sizeof(float));
        pipe.InitBuffer(inQueueY, BUFFER_NUM, this->ubPartDataNum * sizeof(float));

        pipe.InitBuffer(outQueueZ, BUFFER_NUM, this->ubPartDataNum * sizeof(float)*2);

    }
    
    __aicore__ inline void Process()
    {
        int32_t loopCount = this->tileNum;
        this->processDataNum = this->ubPartDataNum;
        for (int32_t i = 0; i < loopCount-1; i++) 
        {
            CopyIn(i);
            Compute(i);
            CopyOut(i);
        }
        this->processDataNum = this->tailDataNum;
        CopyIn(loopCount-1);
        Compute(loopCount-1);
        CopyOut(loopCount-1);
    }

private:
    __aicore__ inline void CopyIn(int32_t progress)
    {
        AscendC::LocalTensor<float> xLocal = inQueueX.AllocTensor<float>();
        AscendC::LocalTensor<float> yLocal = inQueueY.AllocTensor<float>();

        AscendC::DataCopy(xLocal, xGm[progress * this->processDataNum], this->processDataNum);
        AscendC::DataCopy(yLocal, yGm[progress * this->processDataNum], this->processDataNum);

        inQueueX.EnQue(xLocal);
        inQueueY.EnQue(yLocal);
    }
    __aicore__ inline void Compute(int32_t progress)
    {
        AscendC::LocalTensor<float> xLocal = inQueueX.DeQue<float>();
        AscendC::LocalTensor<float> yLocal = inQueueY.DeQue<float>();
        AscendC::LocalTensor<float> zLocal = outQueueZ.AllocTensor<float>();

        for (int i = 0; i < this->processDataNum; i++) {
            float real = xLocal.GetValue(i);
            float imag = yLocal.GetValue(i);
            zLocal.SetValue(2 * i, real);     // 存实部
            zLocal.SetValue(2 * i + 1, imag); // 存虚部
        }
     
        outQueueZ.EnQue<float>(zLocal);
        inQueueX.FreeTensor(xLocal);
        inQueueY.FreeTensor(yLocal);
    }
    
    __aicore__ inline void CopyOut(int32_t progress)
    {
        AscendC::LocalTensor<float> zLocal = outQueueZ.DeQue<float>();
        AscendC::DataCopy(zGm[progress * this->ubPartDataNum*2], zLocal, this->processDataNum*2);
        outQueueZ.FreeTensor(zLocal);
    }
private:
    AscendC::TPipe pipe;
    AscendC::TQue<AscendC::TPosition::VECIN, BUFFER_NUM> inQueueX, inQueueY;
    AscendC::TQue<AscendC::TPosition::VECOUT, BUFFER_NUM> outQueueZ;

    AscendC::GlobalTensor<float> xGm;
    AscendC::GlobalTensor<float> yGm;
    AscendC::GlobalTensor<float> zGm;
    uint64_t coreDataNum;
    uint64_t tileNum;
    uint64_t ubPartDataNum;
    uint64_t tailDataNum;
    uint64_t processDataNum;
    int32_t total;
    ComplexTilingData tiling;
};

extern "C" __global__ __aicore__ void complex_custom(GM_ADDR x, GM_ADDR y, GM_ADDR z,GM_ADDR workspace, GM_ADDR tiling)
{
    GET_TILING_DATA(tiling_data, tiling);
    KernelComplex op;
    op.Init(x, y, z,tiling_data);
    op.Process();
}

